fix(controller): default agent deployments to restricted Pod Security#2251
fix(controller): default agent deployments to restricted Pod Security#2251reyshazni wants to merge 1 commit into
Conversation
Signed-off-by: reyshazni <reyshazni@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the agent manifest translation layer so that controller-generated Agent Deployments default to Kubernetes Pod Security Standards (PSS) restricted-compliant securityContext values when users do not explicitly provide them, preventing admission rejection on restricted-enforced clusters.
Changes:
- Inject restricted-PSS defaults for container security context when none is provided (runAsNonRoot, seccomp RuntimeDefault, drop ALL capabilities, disallow privilege escalation).
- Inject restricted-PSS defaults for pod security context when none is provided (runAsNonRoot, seccomp RuntimeDefault).
- Update and extend unit tests to assert the new defaulting behavior for pod-only, container-only, and fully-nil security context cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| go/core/internal/controller/translator/agent/manifest_builder.go | Adds restricted-PSS defaulting helpers and applies them when security contexts are omitted during pod template construction. |
| go/core/internal/controller/translator/agent/security_context_test.go | Updates existing security-context tests and adds coverage for the fully-nil case to verify restricted defaults are applied. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Spec: corev1.PodSpec{ | ||
| ServiceAccountName: *dep.ServiceAccountName, | ||
| ImagePullSecrets: dep.ImagePullSecrets, | ||
| SecurityContext: dep.PodSecurityContext, | ||
| SecurityContext: defaultPodSecurityContext(dep.PodSecurityContext), | ||
| InitContainers: runtimeInputs.initContainers, |
|
|
||
| if !needCodeExecIsolation { | ||
| return nil | ||
| return defaultRestrictedContainerSecurityContext() |
There was a problem hiding this comment.
forcing runasnonroot true by default breaks byo agents whose images run as root on any cluster, so consider applying these defaults only to kagent owned images or making them opt in.
Problem
Agent Deployments created through the kagent UI or CLI produce pod templates with empty
securityContextat both pod and container level. On clusters that enforce the KubernetesrestrictedPod Security Standard (the recommended default since Kubernetes 1.25), admission rejects these pods before they start. Agents never become ready.The kagent control-plane pods are already compliant, which makes this inconsistency confusing for users.
Fixes #2244
Root Cause
buildContainerSecurityContextreturnsnilwhen the Agent CR has noSecurityContextand code-exec isolation is not needed.buildPodTemplatepassesdep.PodSecurityContextdirectly, which is alsonilby default. The result is a pod spec with no security context at either level.Fix
When the user does not supply security context fields, the controller now injects restricted-PSS-compliant defaults:
Container level:
runAsNonRoot: trueallowPrivilegeEscalation: falsecapabilities.drop: [ALL]seccompProfile: RuntimeDefaultPod level:
runAsNonRoot: trueseccompProfile: RuntimeDefaultUser-supplied values always take precedence. The existing
base != nilpath inbuildContainerSecurityContextand the nil check indefaultPodSecurityContextpreserve any explicit user configuration unchanged.The code-exec isolation path (skills with sandbox) is also unchanged: when
needCodeExecIsolationis true, the controller still setsPrivileged: trueas before.Testing
TestSecurityContext_OnlyPodSecurityContextto verify that the container now receives restricted defaults instead of nilTestSecurityContext_OnlyContainerSecurityContextto verify that the pod now receives restricted defaults instead of nilTestSecurityContext_NilSecurityContext_RestrictedDefaultscovering the common case where no SecurityContext is set at allSkillsDefaultPrivilegedSandbox,SkillsPSSRestricted) continue to pass without modification